package org.yoki.edu.portal.web.utils;

import java.util.Collection;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by kissy on 2017/8/14.
 */
public class CommonUtils {
    public static String getRemoteHost(javax.servlet.http.HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;
    }

    /**
     * 检查对象是否为空
     *
     * @param obj java任意类型
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static boolean isEmpty(Object obj) {
        if (obj == null) {
            return true;

        } else if (obj instanceof String && (obj.toString().trim().equals(""))) {
            return true;

        } else if (obj instanceof Number && ((Number) obj).doubleValue() < 0) {
            return true;

        } else if (obj instanceof Collection && ((Collection) obj).isEmpty()) {
            return true;

        } else if (obj instanceof Map && ((Map) obj).isEmpty()) {
            return true;

        } else if (obj instanceof Object[] && ((Object[]) obj).length == 0) {
            return true;

        }
        return false;
    }

    /**
     * 判断对象是否是基本数据类型
     *
     * @param obj
     * @return
     */
    public static boolean isBaseType(Object obj) {
        if (obj == null) {
            return true;

        } else if (obj instanceof String) {
            return true;

        } else if (obj instanceof Number) {
            return true;

        } else if (obj instanceof Integer) {
            return true;

        } else if (obj instanceof Long) {
            return true;

        } else if (obj instanceof Float) {
            return true;
        } else if (obj instanceof Boolean) {
            return true;
        } else if (obj instanceof Double) {
            return true;
        } else if (obj instanceof Short) {
            return true;
        } else if (obj instanceof Byte) {
            return true;
        }
        return false;
    }

    /**
     * 检查n个对象是否为空
     *
     * @param obj
     * @return
     */
    public static boolean isEmpty(Object... obj) {
        boolean res = false;
        for (Object o : obj) {
            if (isEmpty(o)) {
                res = true;
                break;
            }
        }
        return res;
    }

    /**
     * 检查对象是否不为空
     *
     * @param obj
     * @return
     */
    public static boolean isNotEmpty(Object obj) {
        return !isEmpty(obj);
    }

    /**
     * 检查对象是否不为空
     *
     * @param obj
     * @return
     */
    public static boolean isNotEmpty(Object... obj) {
        boolean res = true;
        for (Object o : obj) {
            if (isEmpty(o)) {
                res = false;
                break;
            }
        }
        return res;
    }

    /**
     * 大陆手机号码11位数，匹配格式：前三位固定格式+后8位任意数
     * 此方法中前三位格式有：
     * 13+任意数
     * 15+除4的任意数
     * 18+除1和4的任意数
     * 17+除9的任意数
     * 147
     */
    public static boolean isChinaPhoneLegal(String str) {
        String regExp = "^((13[0-9])|(15[^4])|(18[0,1,2,3,5-9])|(17[0-8])|(147))\\d{8}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }

}
